package com.sweet.string;/**
 * Created by sweet on 2017-03-03.
 */

import org.apache.commons.lang3.StringUtils;

/**
 * 文本处理工具
 *
 * @author limi
 * @version 2017-03-03-16:04
 */
public class TextUtil {

    public static String escapeBlankCharacter(String source) {
        if (!StringUtils.isBlank(source))
            return source.replaceAll("\n", "\\\\n").replaceAll("\r", "\\\\r");
        else
            return source;
    }

    public static String escapeQuote(String source) {
        if (!StringUtils.isBlank(source))
            return source.replaceAll("\"", "\\\\\"");
        else
            return source;
    }

    public static String dotToNewLine(String source) {
        if (!StringUtils.isBlank(source))
            return source.replaceAll(",", "\\\\n");
        else
            return source;
    }

    public static String brToNewLineCharacter(String source) {
        if (!StringUtils.isBlank(source))
            return source.replaceAll("(<br>)|(<br/>)", "\\\\n");
        else
            return source;
    }

    public static String newLineCharacterToBr(String source) {
        if (!StringUtils.isBlank(source))
            return source.replaceAll("\n", "<br>");
        else
            return source;
    }

    /**
     * 检查内容
     *
     * @param content
     * @return
     */
    public static String filter(String content) {
        //全部转为小写
        content = content.toLowerCase();

        content = StringUtils.replace(content, "script", "s c r i p t");
        content = StringUtils.replace(content, ".js", " j s");
        content = StringUtils.replace(content, "src", "");
        content = StringUtils.replace(content, "href", "");
        content = StringUtils.replace(content, "http://", "");
        content = StringUtils.replace(content, "https://", "");
        content = StringUtils.replace(content, "</", "");
        content = StringUtils.replace(content, "<", "");
        content = StringUtils.replace(content, "/>", "");
        content = StringUtils.replace(content, ">", "");

        return content;
    }

    public static void main(String arg[]) {
        System.out.println(filter("<ScRiPt src=>asdfasdf</>"));
        System.out.println(filter("</ScRipt href=asdfsadf"));
        System.out.println(filter("</.jshttp://"));

        System.out.println(filter("<acript src=>asdfasdf</href=>"));
        System.out.println(filter("</acript href=asdfsadfsrc="));
        System.out.println(filter("</abcdjshttp://"));
    }
}